class Npc {

   

    x = 0;

    y = 0;

    id = 0;

    mapID = 0;

    name = "";
    
    npcModelTexture = new Image();
    npcHeadTexture = new Image();
   
    view = new renderer.Bitmap(0, 0, this.npcModelTexture);
    
    canAcceptMissions: Mission[] = [];

    canSubmitMissions: Mission[] = [];

    constructor() {

        this.view.scaleX = this.view.scaleY = 0.7;

        missionManager.addEventListener("missionUpdate", () => {
            this.update();
        })
        this.update();

    }
    parseFromConfig(config: any) {
        this.id = config.id;
        this.mapID = config.mapId;
        this.name = config.name;
        this.npcHeadTexture.src = config.image;
        this.npcModelTexture.src = config.avatar;
        this.x = config.x;
        this.y = config.y;

    }

    private update() {
        this.canAcceptMissions = [];
        this.canSubmitMissions = [];
        for (let mission of missionManager.missions) {
            if (mission.status == MissionStatus.CAN_ACCEPT && mission.fromNpcId == this.id) {
                this.canAcceptMissions.push(mission);

            }
            if (mission.status == MissionStatus.CAN_SUBMIT && mission.toNpcId == this.id) {
                this.canSubmitMissions.push(mission);
            }
        }
    }


    toString() {
        return `[NPC: id:${this.id}]`
    }
}

class NpcManager extends EventDispatcher{
    npc: Npc[] = [];
    constructor() {
        super();
    }

    parseFromConfig(config: any) {
        for (let item of config.npc) {
            const npc1 = new Npc();
            npc1.parseFromConfig(item);
            npcManager.npc.push(npc1);
           
           
        }
    }
    
}

const npcManager = new NpcManager();

